﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Evpo.ContactData.ViewModel;
using Evpo.ContactData.Common;
using System.Collections.Specialized;
using Evpo.ContactData.UI.Tools;
using Evpo.ContactData.UI.GeneralClasses;

namespace Evpo.ContactData.UI
{
    public partial class ContactList : ItemList
    {
        #region Contants
        #endregion

        #region Properties
        protected override string ItemInListControlName
        {
            get { return "uiItemInList"; }
        }

        protected override ListViewModel BaseViewModel
        {
            get { return ViewModel; }
        }

        protected override ListView ListView
        {
            get { return uiListView; }
        }

        protected ContactListViewModel ViewModel
        {
            get;
            set;
        }

        protected UrlComposer UrlCmp
        {
            get;
            set;
        }

        #endregion

        #region Handlers

        protected void Page_Init(object sender, EventArgs e)
        {
            ContactQueryParameters parameters = 
                UrlComposer.ExtractQueryParameters(Request.QueryString);

            ViewModel = new ContactListViewModel(
                new ModelFactory(),
                (IConnection)Session[SessionStateKeys.Connection],
                parameters,
                Preferences.PageCount);

            UrlCmp = new UrlComposer(ViewModel, Request.Url);

            Page.SaveStateComplete += new EventHandler(Page_SaveStateComplete);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateSortFieldList();

            if (!IsPostBack)
            {
                uiDataPager.DataBind();
                DataBind();
            }
        }

        protected void Page_SaveStateComplete(object sender, EventArgs e)
        {
            if (ViewModel.ItemCount == 0)
                return;

            Panel uiLeftColumn = (Panel)uiListView.FindControl("uiLeftColumn");
            Panel uiRightColumn = (Panel)uiListView.FindControl("uiRightColumn");
            
            int num = (int)Math.Ceiling(double.Parse(uiPageSize.SelectedValue) / 2);

            num = uiListView.Items.Count - num;

            for (int i = uiLeftColumn.Controls.Count - 1; i >= 0; i--)
            {
                ListViewDataItem item = uiLeftColumn.Controls[i] as ListViewDataItem;
                if (item == null)
                    continue;

                if (num <= 0)
                    break;

                uiLeftColumn.Controls.Remove(item);

                uiRightColumn.Controls.AddAt(0, item);

                num--;
            }
        }

        protected void uiPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            uiDataPager.SetPageProperties(0, int.Parse(uiPageSize.SelectedValue), true);
        }

        protected void uiFilterByName_Click(object sender, EventArgs e)
        {
            ViewModel.ContactQuery.FirstNameContains = uiFirstName.Text;
            ViewModel.ContactQuery.LastNameContains = uiLastName.Text;
            ViewModel.GoToFirstPage();
            Server.Transfer(UrlCmp.CurrentUrl.PathAndQuery);
        }


        #endregion

        #region Fields
        #endregion Fields

        #region Methods

        private void PopulateSortFieldList()
        {
            uiSort.Items.AddRange(
                new ListItem[]
                {
                new ListItem("Default", UrlCmp.UrlForSortField(ContactSortFields.Default).ToString())
                {
                    Selected = ViewModel.ContactQuery.SortField == ContactSortFields.Default
                },
                new ListItem("First Name", UrlCmp.UrlForSortField(ContactSortFields.FirstName).ToString())
                {
                    Selected = ViewModel.ContactQuery.SortField == ContactSortFields.FirstName
                },
                new ListItem("Last Name", UrlCmp.UrlForSortField(ContactSortFields.LastName).ToString())
                {
                    Selected = ViewModel.ContactQuery.SortField == ContactSortFields.LastName
                },
                });
        }



        public Uri UrlFromContactItem(ContactListItem contactItem)
        {
            return UrlCmp.ContactItemUrl(contactItem.Contact.ContactID);
        }

        #endregion

        #region Classes
        protected class QueryKeys
        {
            internal const string PageBegin = "PageBegin";
            internal const string PageEnd = "PageEnd";
            internal const string FirstNameContains = "FirstNameContains";
            internal const string LastNameContains = "LastNameContains";
            internal const string SortField = "SortField";
        }
        #endregion



    }
}